package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.ChatMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;

import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

@Controller
public class WsController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMsg chatMsg){

        Admin admin =(Admin)authentication.getPrincipal();
       chatMsg.setFrom(admin.getUsername());
     /*   chatMsg.setFrom("1");*/
        chatMsg.setFormNickName(admin.getName());
      /*  chatMsg.setFormNickName("2");*/
        chatMsg.setDate(LocalDateTime.now());
        //simpMessagingTemplate.convertAndSend("/queue1/chat",chatMsg);
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);
    }
}
